{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cohere\n",
    "\n",
    "This notebook demonstrates how to use Cohere Embeddings with Chroma.\n",
    "\n",
    "If you have not already, [create a Cohere account](https://dashboard.cohere.ai/welcome/register) and get your API Key.\n",
    "\n",
    "First a basic example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.1.2\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49m/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip\u001b[0m\n",
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.1.2\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49m/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "! pip install chromadb --quiet\n",
    "! pip install cohere --quiet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import getpass\n",
    "\n",
    "os.environ[\"COHERE_API_KEY\"] = getpass.getpass(\"Cohere API Key:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'ids': [['3']], 'embeddings': None, 'documents': [['I like oranges']], 'metadatas': [[{'fruit': 'orange'}]], 'distances': [[6729.3291015625]]}\n"
     ]
    }
   ],
   "source": [
    "import chromadb\n",
    "from chromadb.utils import embedding_functions\n",
    "\n",
    "cohere_ef = embedding_functions.CohereEmbeddingFunction(api_key=os.environ[\"COHERE_API_KEY\"],  model_name=\"large\")\n",
    "\n",
    "client = chromadb.Client()\n",
    "collection = client.create_collection(\"cohere_python\", embedding_function=cohere_ef)\n",
    "\n",
    "collection.add(\n",
    "    ids=[\"1\", \"2\", \"3\"],\n",
    "    documents=[\"I like apples\", \"I like bananas\", \"I like oranges\"],\n",
    "    metadatas=[{\"fruit\": \"apple\"}, {\"fruit\": \"banana\"}, {\"fruit\": \"orange\"}],\n",
    ")\n",
    "\n",
    "print(collection.query(query_texts=[\"citrus\"], n_results=1))\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multilingual Example\n",
    "\n",
    "Cohere can support many languages! In this example we store text in many languages, and then query in English."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'ids': [['9']], 'embeddings': None, 'documents': [['나는 오렌지를 좋아한다']], 'metadatas': [[None]], 'distances': [[30.728900909423828]]}\n"
     ]
    }
   ],
   "source": [
    "cohere_mutlilingual = embedding_functions.CohereEmbeddingFunction(\n",
    "        api_key=os.environ[\"COHERE_API_KEY\"], \n",
    "        model_name=\"multilingual-22-12\")\n",
    "\n",
    "# 나는 오렌지를 좋아한다 is \"I like oranges\" in Korean\n",
    "multilingual_texts = [ 'Hello from Cohere!', 'مرحبًا من كوهير!', \n",
    "        'Hallo von Cohere!', 'Bonjour de Cohere!', \n",
    "        '¡Hola desde Cohere!', 'Olá do Cohere!', \n",
    "        'Ciao da Cohere!', '您好，来自 Cohere！',\n",
    "        'कोहेरे से नमस्ते!', '나는 오렌지를 좋아한다'  ]\n",
    "\n",
    "collection = client.create_collection(\"cohere_multilingual\", embedding_function=cohere_mutlilingual)\n",
    "\n",
    "collection.add(\n",
    "    ids=[str(i) for i in range(len(multilingual_texts))],\n",
    "    documents=multilingual_texts\n",
    ")\n",
    "\n",
    "print(collection.query(query_texts=[\"citrus\"], n_results=1))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
